<?php
/**
 * Created by PhpStorm.
 * User: xuho
 * Date: 2020-02-15
 * Time: 19:37
 */

namespace app\cdc\validate\admin;


use app\common\model\mysql\ConfigsModel;

class ConfigValidate extends \app\common\validate\BaseValidate
{
    protected $regex = ['no_number' => '/^(?![0-9]+$)/i'];

    protected $rule = [
        'title' => 'require',
        'type' => 'require',
        'name' => 'require|no_number',
        'group' => 'require|checkConfig',
    ];

    protected $message = [
        'title.require' => '变量标题 必选',
        'type.require' => '变量值类型 必选',
        'name.require' => '变量名 必填',
        'name.no_number' => '变量名不能单纯是数字',
        'group.require' => '配置组 必填',
    ];

    protected $scene = [
        'save' => ['title', 'type', 'status', 'name', 'group'],
    ];

    protected function checkConfig($value, $rule, $data = [])
    {
        if (ConfigsModel::checkRowExists(['group' => $data['group'], 'name' => $data['name']])) {
            return '该组下配置已存在';
        }
        return true;
    }
}